package com.lorentzos.swipecards.example;

import com.lorentzos.flingswipe.CustomPager;
import com.lorentzos.flingswipe.PagerProvider;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;

public class MainAbility extends Ability {
    private CustomPager pageSlider;
    private PagerProvider provider;
    private int index=1;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(MainAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_main);
        pageSlider= (CustomPager) findComponentById(ResourceTable.Id_scl_layout);
        getData();
        provider=new PagerProvider(getData(),this);
        pageSlider.setProvider(provider);
        pageSlider.setSlidingPossible(false);
        pageSlider.setFlingListener(new CustomPager.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                provider.getList().remove(0);
                provider.notifyDataChanged();
            }

            @Override
            public void onLeftCardExit(Object dataObject) {
//                    getUITaskDispatcher().asyncDispatch(new Runnable() {
//                        @Override
//                        public void run() {
//                            new ToastDialog(MainAbility.this).setContentText("Left!").show();
//                        }
//                    });
            }

            @Override
            public void onRightCardExit(Object dataObject) {
//                getUITaskDispatcher().asyncDispatch(new Runnable() {
//                    @Override
//                    public void run() {
//                        new ToastDialog(MainAbility.this).setContentText("Right!").show();
//                    }
//                });
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                provider.getList().add(new PagerProvider.DataItem("第"+index+"个页面"));
                provider.notifyDataChanged();
                index++;
            }

            @Override
            public void onScroll(float scrollProgressPercent) {

            }
        });
        findComponentById(ResourceTable.Id_left).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                pageSlider.selectLeft();
            }
        });
        findComponentById(ResourceTable.Id_right).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                pageSlider.selectRight();
            }
        });
        findComponentById(ResourceTable.Id_test).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName("com.lorentzos.swipecards.example")
                        .withAbilityName("com.lorentzos.swipecards.example.TwoDemoAbility")
                        .build();
                intent1.setOperation(operation);
                startAbility(intent1);
            }
        });
    }
    private ArrayList<PagerProvider.DataItem> getData() {
        ArrayList<PagerProvider.DataItem> dataItems = new ArrayList<>();
        dataItems.add(new PagerProvider.DataItem("第一个页面"));
        dataItems.add(new PagerProvider.DataItem("第二个页面"));
        dataItems.add(new PagerProvider.DataItem("第三个页面"));
        dataItems.add(new PagerProvider.DataItem("第四个页面"));
        return dataItems;
    }
}
